<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 导航管理
 *
 * @icon fa fa-circle-o
 */
class Navigation extends Backend
{
    
    /**
     * Navigation模型对象
     * @var \app\admin\model\Navigation
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Navigation;
        $this->model = model('Navigation');
        // 必须将结果集转换为数组
        $ruleList = Db::name("navigation")->field('is_self,created_at,updated_at', true)->select();
        foreach ($ruleList as $k => &$v) {
            $v['title'] = __($v['title']);
        }
        unset($v);
        Tree::instance()->init($ruleList);
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        $ruledata = [0 =>'顶级'];
        foreach ($this->rulelist as $k => &$v) {
            if ($v['location'] == 1){
                $ruledata[$v['id']] = $v['title'].'&nbsp;头部';
            }else{
                $ruledata[$v['id']] = $v['title'].'&nbsp;尾部';
            }
            unset($v['spacer']);
        }
        unset($v);
        $this->view->assign('ruledata', $ruledata);
        $this->view->assign("locationList", $this->model->getLocationList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isSelfList", $this->model->getIsSelfList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function sort($cateRes, $pid = 0, $level = 0)
    {
        static $arr = [];
        foreach ($cateRes as $k => $v) {
            //第一次寻找顶级栏目 默认pid顶级为0 已赋默认值
            if ($v['pid'] == $pid) {
                $v['level'] = $level;
                $arr[] = $v;
                $this->sort($cateRes, $v['id'], $level + 1);
            }
        }
        return $arr;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $data = $this->sort($list);
            foreach ($data as &$v){
                $v['title'] = str_repeat('&nbsp;',$v['level']*2 ) .str_repeat('-',$v['level']*2 ). $v['title'];
            }
            $result = array("total" => $list->total(), "rows" => $data);

            return json($result);
        }
        return $this->view->fetch();
    }

}
